package com.sikaryofficial.backend.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sikaryofficial.backend.domain.dto.req.EvaluateReplyListReq;
import com.sikaryofficial.backend.domain.dto.req.EvaluateReplyReq;
import com.sikaryofficial.backend.domain.dto.resp.EvaluateReplyDTO;
import com.sikaryofficial.backend.domain.entity.Attachment;
import com.sikaryofficial.backend.domain.entity.BrandTasteEvaluateReply;
import com.sikaryofficial.backend.domain.mapping.EvaluateReplyMapping;
import com.sikaryofficial.backend.domain.vo.EvaluateReplyCount;
import com.sikaryofficial.backend.mapper.BrandTasteEvaluateReplyMapper;
import com.sikaryofficial.backend.service.IAttachmentService;
import com.sikaryofficial.backend.service.IBrandTasteEvaluateReplyService;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 品牌口味评价回复Service业务层处理
 *
 * @author qinjinyuan
 * @date 2023-11-03
 */
@Service
public class BrandTasteEvaluateReplyServiceImpl extends ServiceImpl<BrandTasteEvaluateReplyMapper, BrandTasteEvaluateReply> implements IBrandTasteEvaluateReplyService {
    @Autowired
    private BrandTasteEvaluateReplyMapper brandTasteEvaluateReplyMapper;

    @Autowired
    private IAttachmentService attachmentService;

    /**
     * 回复分页列表
     *
     * @param customerPage
     * @param req
     * @return
     */
    @Override
    public IPage<EvaluateReplyDTO> replyList(Page<EvaluateReplyDTO> customerPage, EvaluateReplyListReq req) {
        return brandTasteEvaluateReplyMapper.replyList(customerPage,req, req.getUserId());
    }

    /**
     * 删除跟评
     *
     * @param evaluateIds
     * @return
     */
    @Override
    public boolean removeReply(Long[] evaluateIds) {

        Long userId = SecurityUtils.getUserId();
        LambdaUpdateWrapper<Attachment> attachmentWrapper
                = new LambdaUpdateWrapper<>();
        attachmentWrapper.set(Attachment::getUpdatedBy, userId)
                .set(Attachment::getUpdatedTime, new Date())
                .setSql(" deleted_version = evaluate_reply_id ")
                .in(Attachment::getObjectId, Arrays.asList(evaluateIds))
                .eq(Attachment::getDeletedVersion, 0L);
        attachmentService.update(attachmentWrapper);

        LambdaUpdateWrapper<BrandTasteEvaluateReply> updateWrapper
                = new LambdaUpdateWrapper<>();
        updateWrapper.set(BrandTasteEvaluateReply::getUpdatedBy, userId)
                .set(BrandTasteEvaluateReply::getUpdatedTime, new Date())
                .setSql(" deleted_version = evaluate_reply_id ")
                .in(BrandTasteEvaluateReply::getEvaluateId, Arrays.asList(evaluateIds))
                .eq(BrandTasteEvaluateReply::getDeletedVersion, 0L);
        return this.update(updateWrapper);
    }

    /**
     * 回复保存
     *
     * @param evaluateReplyReq
     * @return
     */
    @Override
    public EvaluateReplyDTO saveReply(EvaluateReplyReq evaluateReplyReq) {
        Long replayId = IdWorker.getId();
        evaluateReplyReq.setEvaluateReplyId(replayId);

        BrandTasteEvaluateReply evaluateReply = BrandTasteEvaluateReply.builder()
                .createdBy(SecurityUtils.getUserId())
                .createdTime(new Date())
                .evaluateReplyId(replayId)
                .evaluateId(evaluateReplyReq.getEvaluateId())
                .replyContent(evaluateReplyReq.getReplyContent())
                .createdName(SecurityUtils.getUsername())
                .build();
        attachmentService.saveAttachmentList(replayId, evaluateReplyReq.getAttachmentList(),
                SecurityUtils.getUserId());
        this.save(evaluateReply);

        BrandTasteEvaluateReply evaluateReply1 = this.getById(replayId);
        EvaluateReplyDTO result = EvaluateReplyMapping.INSTANCE.coverToDTO(evaluateReply1);
        result.setAttachmentList(attachmentService.getAttachmentById(replayId));
        return result;
    }

    /**
     * 分组统计评论下ID对应的回复数量
     *
     * @param evaluateIds
     * @return
     */
    @Override
    public List<EvaluateReplyCount> countByEvaluateId(List<Long> evaluateIds) {
        if (CollUtil.isEmpty(evaluateIds)) {
            return Collections.emptyList();
        }
        return brandTasteEvaluateReplyMapper.countByEvaluateId(evaluateIds);
    }
}
